package org.abettor.pushbox.activity2;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.download.DownloadMap;
import org.abettor.pushbox.download.ParaDownloadRetValue;
import org.abettor.pushbox.model.LastTimeIdInterface;
import org.abettor.pushbox.model.SelfMessage;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.abettor.pushbox.model.MessageBoardBean;
import org.abettor.pushbox.upload.SendComments;
import org.abettor.pushbox.R;

public class MessageBoardActivity extends AbstractSendListActivity {
	private ProgressDialog progressDialog;// 进度对话框
	private DownloadMap downloadMap;
	private SendComments sendComments;
	private int map_id = -1;// 地图的id

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(R.string.label_map_comment);
		// 获得地图的id
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		map_id = extras.getInt("_id");
		downloadMap = new DownloadMap(this);
		sendComments = new SendComments(this);
		//加载数据
		loadNewstTList();
	}

	@Override
	protected void showOldProgressBar() {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				progressDialog = new ProgressDialog(MessageBoardActivity.this);
				progressDialog
						.setTitle(R.string.abstract_sendlist_activity_loadmsg);
				progressDialog.setMessage(MessageBoardActivity.this
						.getText(R.string.abstract_sendlist_activity_loadnetdata_loading));
				progressDialog.show();
			}
		});
	}

	@Override
	protected void dimissOldProgressBar() {
		progressDialog.dismiss();
	}

	@Override
	protected View getListView(int position, View convertView,
			ViewGroup parent, LastTimeIdInterface bean) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		MessageBoardBean item = (MessageBoardBean) bean;
		LayoutInflater inflater = MessageBoardActivity.this.getLayoutInflater();
		ViewGroup theParent = (ViewGroup) inflater.inflate(
				R.layout.message_board_activity_item, parent, false);
		TextView evaTitle = (TextView) theParent
				.findViewById(R.id.evaluation_title);
		evaTitle.setText(changeMsgToStrTitle(item));
		TextView evaContent = (TextView) theParent
				.findViewById(R.id.evaluation_content);
		evaContent.setText(changeMsgToStrContent(item));
		TextView updateTime = (TextView) theParent
				.findViewById(R.id.update_time);
		updateTime.setText(sdf.format(item.getTime()));
		return theParent;
	}

	private String changeMsgToStrTitle(MessageBoardBean item) {
		String str = "";
		str = str
				+ MessageBoardActivity.this
						.getText(R.string.message_board_activity_evalor_label)
				+ ":";
		String nickName = item.getNickName();
		SelfMessage self = Config.readSelfMessage(this);
		if (self != null && self.getNickName().equals(nickName)) {
			nickName = nickName + "("
					+ MessageBoardActivity.this.getText(R.string.message_board_activity_me) + ")";
		}
		str = str + nickName;
		return str;
	}

	private String changeMsgToStrContent(MessageBoardBean item) {
		String str = "";
		str = str
				+ MessageBoardActivity.this
						.getText(R.string.message_board_activity_content_label)
				+ ":";
		str = str + item.getContent();
		return str;
	}

	@Override
	protected String loadEarlyStrFromNet(Date date) {
		String ret = downloadMap.downloadEarlyMessageBoardListStr(map_id, date,
				Config.PAGESIZE);
		return ret;
	}

	@Override
	protected int isHasMoreResult(String content) {
		return ParaDownloadRetValue.isHasMoreNetMap(content);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected List pareMoreResult(String content) {
		return ParaDownloadRetValue.paraMessageBoardBean(content);
	}

	@Override
	protected String loadOldStrFromNet(Date date) {
		String ret = downloadMap.downloadNewestMessageBoardListStr(map_id, date,
				Config.PAGESIZE);
		return ret;
	}

	@Override
	protected boolean sendMsgOperation(String msg) {
		return sendComments.sendComments(map_id, msg);
	}

}
